package com.cuz.hot100;

import com.cuz.daileetcode.utils.ArrayUtils;
public class Q48RotateImage {

    public static void main(String[] args) {
        class A{
            public void rotate(int[][] matrix) {


                int row = matrix.length;
                int col = matrix[0].length;
                int top = 0;
                int deep = row-1;
                while(top<deep){
                    for(int i = 0;i<col;i++){
                        int temp = matrix[top][i];
                        matrix[top][i] = matrix[deep][i];
                        matrix[deep][i]=temp;
                    }
                    top++;
                    deep--;
                }

                for(int i=0;i<row;i++){
                    for(int j =i+1;j<col;j++){
                        int temp = matrix[i][j];
                        matrix[i][j] = matrix[j][i];
                        matrix[j][i] = temp;
                    }
                }

            }
        }

        int[][]ints2 = new int[][]{
                {1,2,3},
                {4,5,6},
                {7,8,9}
        };
        new A().rotate(ints2);


        int[][] ints = {
               //0  1  2  3  4
                {1, 2, 3, 4, 5},
                {6, 7, 8, 9, 10},
                {11, 12, 13, 14, 15},
                {16, 17, 18, 19, 20},
                {21, 22, 23, 24, 25}
        };
        for (int i = 0; i <= ints.length / 2; i++) {
            move2(i+1,ints.length-i-1, ints.length, ints);
        }

        ArrayUtils.print(ints);
    }
    class Solution {
        public void rotate(int[][] matrix) {
            if(matrix.length == 0 || matrix.length != matrix[0].length) {
                return;
            }
            int nums = matrix.length;
            int times = 0;
            while(times <= (nums >> 1)){
                int len = nums - (times << 1);
                for(int i = 0; i < len - 1; ++i){
                    int temp = matrix[times][times + i];
                    matrix[times][times + i] = matrix[times + len - i - 1][times];
                    matrix[times + len - i - 1][times] = matrix[times + len - 1][times + len - i - 1];
                    matrix[times + len - 1][times + len - i - 1] = matrix[times + i][times + len - 1];
                    matrix[times + i][times + len - 1] = temp;
                }
                ++times;
            }
        }
    }

    //                        1               5
    private static void move2(int times, int end, int len, int[][] matrix) {
        while (times <= end) {
            int i14 = matrix[times][len - times];
            int i01 = matrix[times - 1][times];
            int i30 = matrix[len - times - 1][times - 1];
            int i43 = matrix[len - times][len - times - 1];
            matrix[times][len - times] = i01;
            matrix[len - times][len - times - 1] = i14;
            matrix[len - times - 1][times - 1] = i43;
            matrix[times - 1][times] = i30;
            times++;
        }
    }

    public void rotate(int[][] matrix) {
        if (matrix.length == 0 || matrix.length != matrix[0].length) {
            return;
        }
        int nums = matrix.length;
        int times = 0;
        while (times <= (nums / 2)) {
            int len = nums - (times / 2);
            for (int i = 0; i < len - 1; ++i) {
                int temp = matrix[times][times + i];
                matrix[times][times + i] = matrix[times + len - i - 1][times];
                matrix[times + len - i - 1][times] = matrix[times + len - 1][times + len - i - 1];
                matrix[times + len - 1][times + len - i - 1] = matrix[times + i][times + len - 1];
                matrix[times + i][times + len - 1] = temp;
            }
            ++times;
        }
    }

    private static void move(int x, int l, int[][] arr) {
        if (x >= l - 1) {
            return;
        }
        //04 00
        // 44 04
        // 40 44
        // 00 40
        int i40 = arr[l - 1][x];
        int i00 = arr[x][x];
        int i04 = arr[x][l - 1];
        int i44 = arr[l - 1][l - 1];
        arr[x][l - 1] = i00;
        arr[l - 1][l - 1] = i04;
        arr[l - 1][x] = i44;
        arr[x][x] = i40;
    }
}
